#include "SHT40.h"

static const char *TAG = "SHT40_APP";

// 初始化 I2C 总线
static esp_err_t i2c_master_init(void) {
    int i2c_master_port = I2C_MASTER_NUM;
    
    i2c_config_t conf = {
        .mode = I2C_MODE_MASTER,
        .sda_io_num = I2C_MASTER_SDA_IO,
        .scl_io_num = I2C_MASTER_SCL_IO,
        .sda_pullup_en = GPIO_PULLUP_ENABLE,
        .scl_pullup_en = GPIO_PULLUP_ENABLE,
        .master.clk_speed = I2C_MASTER_FREQ_HZ,
    };
    esp_err_t err = i2c_param_config(i2c_master_port, &conf);
    if (err != ESP_OK) {
        return err;
    }

    err = i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
    if (err != ESP_OK) {
    }

    return err;
}

// 向 SHT40 发送命令
static esp_err_t SHT40_write_command(uint8_t command) {
    uint8_t command_buf[] = {command};
    return i2c_master_write_to_device(I2C_MASTER_NUM, SHT40_SENSOR_ADDR, command_buf, sizeof(command_buf), I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
}

// 从 SHT40 读取数据
static esp_err_t SHT40_read_data(uint8_t *data, size_t len) {

    return i2c_master_read_from_device(I2C_MASTER_NUM, SHT40_SENSOR_ADDR, data, len, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
}

// 读取温度数据
 float SHT40_read_temperature(float temperature) {
    uint8_t data[3];
    esp_err_t err;

    // 发送温度测量命令
    err = SHT40_write_command(SHT40_MEASURE_TEMP_CMD);
    if (err != ESP_OK) {
        return err;
    }

    vTaskDelay(pdMS_TO_TICKS(10)); // 等待 SHT40 测量完成

    err = SHT40_read_data(data, sizeof(data));
    if (err != ESP_OK) {
        return err;
    }

    uint16_t temp_raw = (data[0] << 8) | (data[1] & 0xFC);  // 去除状态位
    temperature = -45.0f + 175.0f * ((float)temp_raw / 65535.0f);  // 转换为摄氏度
     if(temperature == -1){
        return SHT40_read_temperature(temperature);
    }
    return temperature;
}

// 读取湿度数据
 float SHT40_read_humidity(float humidity) {
    uint8_t data[3];
    esp_err_t err;

    // 发送湿度测量命令
    err = SHT40_write_command(SHT40_MEASURE_HUM_CMD);
    if (err != ESP_OK) {
        return err;
    }

    vTaskDelay(pdMS_TO_TICKS(10)); // 等待 SHT40 测量完成

    err = SHT40_read_data(data, sizeof(data));
    if (err != ESP_OK) {
        return err;
    }

    uint16_t hum_raw = (data[0] << 8) | (data[1] & 0xFC);  // 去除状态位
    humidity = 100.0f * ((float)hum_raw / 65535.0f);  // 转换为湿度百分比
    if(humidity == -1){
        return SHT40_read_humidity(humidity);
    }
    return humidity;
}



void SHT40_init(){
     ESP_LOGI(TAG, "Initializing I2C...");
    esp_err_t err = i2c_master_init();
    if (err != ESP_OK) {
        return;
    }
}
float SHT40_readTemp(){
        float temperature=0;
        
        // 读取温度
        do {
        temperature = SHT40_read_temperature(temperature); // 获取温度并返回结果
        if (temperature == -1) {
            vTaskDelay(pdMS_TO_TICKS(10)); 
        }
    } while (temperature == -1); // 当读取失败时继续循环
       
        return temperature;
}

float SHT40_readHumi(){
    float humidity=0;
        // 读取湿度
         
        do {
        humidity = SHT40_read_humidity(humidity); // 获取温度并返回结果

        if (humidity == -1) {
            vTaskDelay(pdMS_TO_TICKS(10)); 
        }


    } while (humidity == -1); // 当读取失败时继续循环
    
   return humidity;
}